package middleware

import (
	"Walker/app/http/model"
	"Walker/app/utils/data"
	"Walker/app/utils/response"
	"Walker/global"
	"github.com/gin-gonic/gin"
)

func CasBin(ctx *gin.Context)  {
	//处理权限能力
	//获取数据
	if global.ConfigValue.GetBool("Casbin.isInit") ==true{
        action:=ctx.Request.URL
        method:=ctx.Request.Method
        //获取权限校色
        roleId:=(&model.AdminUser{}).GetRole(ctx.GetUint("UID"))
        ok, err :=global.Enforcer.Enforce(data.IntToString(roleId),action.String(),method)
        if err !=nil {
            response.Error(ctx,err.Error())
            return
        }
        if ok==false{
            response.Error(ctx,"权限不足不允许访问")
            return
        }
	}
	ctx.Next()
}
